//运维电子合同下载记录列表

interface IoperationEContractDownloadViewModel extends IBaseViewModel<IEContractDownloadQueryTerms> {
    /**
     * ui-grid控制
     */
    gridOptions: uiGrid.IGridOptionsOf<EContractDownloadResponse>;
    /**
     * 时间日期格式
     */
    dateFormat: "{format:'YYYY.MM.DD HH:mm'}";
    /**
     * 返回
     */
    skipBack:()=>void;
    /**
     * 列表高度
     */
    heightA: number;

    /**
     * 序号
     */
    index: number;
    /**
     * 合同号
     */
    contractNumber: string;
    /**
     * 客户单位
     */
    clientName: string;
    /**
     * 发货地址
     */
    origin: string;
    /**
     * 送货地址
     */
    destination: string;
    /**
     * 发货时间
     */
    deliveryTime: string;
    /**
     * 车牌号
     */
    carCode: string;
    /**
     * 客服专员
     */
    customerName: string;
    /**
     * 合同状态
     */
    eContractStatus: string;
    /**
     * 补充协议状态
     */
    agreementEContractStatus: string;
    /**
     * 合同订立日期
     */
    contractTime: string;


    /**
     * 电子合同类型
     */
    eContractType: string;

    /**
    * 电子合同编号
    */
    electronicContractNumber: string;
    /**
     * 电子合同状态
     */
    electronicContractStatus: string;
    /**
     * 补充协议合同编号
     */
    agreementContractNumber: string;
    /**
     * 补充协议合同状态
     */
    agreementContractStatus: string;



    /**
     * 客服专员下拉
     */
    customerServiceDropDown: EmployeeListItemResponse[];

    /**
    * 合同状态下拉
    */
    econtractTypeDropDown: ValueListItemResponse[];
    /**
 * 是否显示电子合同
 */
    isContractO: boolean;
    /**
     * 是否显示补充协议
     */
    isAgreementO: boolean;

}

interface IEContractDownloadQueryTerms {

    /**
     * 客户单位名称
     */
    clientName: string;
    /**
     * 合同编号
     */
    contractNumber: string;
    /**
     * 电子合同类型
     */
    econtractType: string;
    /**
     * 客服专员
     */
    customerServiceId: string;
    /**
     * 下载起始时间
     */
    downloadStartTime: string;
    /**
     * 下载结束时间
     */
    downloadEndTime: string;
}


class IEContractDownloadController extends ListController<IoperationEContractDownloadViewModel, IEContractDownloadQueryTerms>{
    gridApi: uiGrid.IGridOptionsOf<EContractDownloadResponse>;
    orderBy: number = 0;
    asc: boolean = true;

    /**
     * 单选
     */
    // selectData: EContractDownloadResponse;
    skip: number = 0;

    constructor($scope: IoperationEContractDownloadViewModel, private valueService: IValueService, private employeeService: IEmployeeService,
        private $ngBootbox: BootboxService, private $state: angular.ui.IStateService, private routeService: routeService, private operationService: IOperationService,
        private $location: ng.ILocationService, private orderService: IorderService) {
        super($scope);
        this.init();
    }

    /**
     * 初始化
     */
    init(): void {
        super.init();
        this.$scope.skipBack = this.skipBack;

        this.$scope.queryTerms = {
            clientName: "",
            contractNumber: "",
            econtractType: "",
            customerServiceId: "",
            downloadStartTime: "",
            downloadEndTime: ""
        };

        this.$scope.gridOptions = {
            multiSelect: false,//单选
            enableRowSelection: true,
            enableRowHeaderSelection: false,
            paginationPageSizes: [10, 20, 30],
            useExternalPagination: true,
            useExternalSorting: true,
            enableColumnResizing: true,
            columnDefs: [
                { displayName: "序号", field: 'index', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '3%' },
                { displayName: "合同号", field: 'contractNumber', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "合同类型", field: 'eContractType', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "客户单位", field: 'clientName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: "发货时间", field: 'deliveryTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '客服专员', field: 'customerServiceName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
                { displayName: '操作人', field: 'operatorName', enableColumnMenu: false, cellTooltip: true, headerTooltip: true, width: '6%' },
                { displayName: '下载时间', field: 'creationTime', enableColumnMenu: false, cellTooltip: true, headerTooltip: true },
               
            ],
            onRegisterApi: (gridApi) => {
                gridApi.pagination.on.paginationChanged(this.$scope, this.paginationChanged);
                // gridApi.selection.on.rowSelectionChanged(this.$scope, this.selectChange);
            }

        }
        $('#openStartTime').click(function () {
            $('#downloadStartTime').datetimepicker('show');
        });
        $('#openEndTime').click(function () {
            $('#downloadEndTime').datetimepicker('show');
        });


        this.query();

        this.loadCustomerServiceDropDown();
        this.loadContractTypeDropDown();


    }

    localHistory(): void {
        super.localHistory(this.$state)
    }
    getHistory(): void {
        super.getHistory(this.$state, this.$location)
    }


    /**
     * 加载数据
     */
    loadData(queryTerms: IEContractDownloadQueryTerms, skip: number, count: number): void {
        // if (this.queryTerms.econtractStatus == null || this.queryTerms.econtractStatus == undefined || this.queryTerms.econtractStatus == "") { this.queryTerms.econtractStatus = "1,2,3,4,5" };
        // if (this.queryTerms.agreementEContractStatus == null || this.queryTerms.agreementEContractStatus == undefined || this.queryTerms.agreementEContractStatus == "") { this.queryTerms.agreementEContractStatus = "1,2,3,4,5" };
        this.operationService.getDownloadRecordList(this.queryTerms.clientName, this.queryTerms.contractNumber,
            this.queryTerms.econtractType, this.queryTerms.customerServiceId, this.queryTerms.downloadStartTime, this.queryTerms.downloadEndTime, skip, count).then((result) => {
                this.$scope.gridOptions.totalItems = result.data.total;
                this.$scope.gridOptions.data = result.data.data;
                result.data.data.map((item, index) => item.index = skip++ +1);
                // let gridHeight = 0;
                // result.data.data.forEach((item, index) => {
                //      gridHeight = gridHeight + item.orderList.length;
                // });
                this.$scope.heightA = (this.$scope.gridOptions.data.length * 39) + 96;

                if (skip == 0) {
                    this.$scope.gridOptions.paginationCurrentPage = 1;
                }
                this.$scope.querying = false;
            }, error => {
                this.$scope.querying = false;
            });
    }


    /**
     * 客服专员下拉
     */
    loadCustomerServiceDropDown(): void {
        this.employeeService.getbytype("4", 0, -1).then(result => {//CommissionerType.CustomerService.toString()
            this.$scope.customerServiceDropDown = result.data.data;
        });
    }

    /**
     * 合同类型下拉
     */
    loadContractTypeDropDown(): void {
        this.$scope.econtractTypeDropDown = this.valueService.getEContractType().data;
    }

    //返回
    skipBack = () => {
        this.$state.go("app.opera.operation_EContractList")
    }

    /**
     * 单选
     */
    // selectChange = (row: uiGrid.IGridRowOf<EContractDownloadResponse>) => {
    //     if (row.isSelected) {
    //         this.selectData = row.entity;

    //     } else {
    //         this.selectData = null;

    //     }
    // }


}
angular.module("tms").controller("EContractDownloadController", ["$scope", "valueService", "employeeService", "$ngBootbox", "$state", "routeService", "operationService", "$location", "orderService", IEContractDownloadController]);
